package com.tywj.lib.core.widget.recycleview.adapter

import android.util.SparseIntArray
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.chad.library.adapter.base.entity.MultiItemEntity

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/13
 *     desc   : RecyclerView多Item类型适配器
 *     version: 1.0.0
 * </pre>
 */
abstract class BaseMultiItemAdapter<T : MultiItemEntity> : BaseAdapter<T>() {

    companion object {
        private const val DEFAULT_VIEW_TYPE = -0xff
    }

    private var layouts = SparseIntArray()

    override fun getDefItemViewType(position: Int): Int {
        val item = mData[position]
        if (item is com.chad.library.adapter.base.entity.MultiItemEntity) {
            return (item as com.chad.library.adapter.base.entity.MultiItemEntity).itemType
        }
        return DEFAULT_VIEW_TYPE
    }

    protected fun setDefaultViewTypeLayout(@LayoutRes layoutResId: Int) {
        addItemType(DEFAULT_VIEW_TYPE, layoutResId)
    }

    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder =
            createBaseViewHolder(parent, getLayoutId(viewType))

    private fun getLayoutId(viewType: Int): Int = layouts.get(viewType)

    protected fun addItemType(type: Int, @LayoutRes layoutResId: Int) {
        layouts.put(type, layoutResId)
    }

}